<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <base th:href="${#servletContext.getContextPath()+'/'}">

    <script type="text/javascript" src="https://cdn.goeasy.io/goeasy-2.0.13.min.js"></script>


    <script typ|e="text/javascript">

        // 1、初始化,建立连接
        var goeasy  = GoEasy.getInstance({
            host:"hangzhou.goeasy.io",  //若是新加坡区域：singapore.goeasy.io
            appkey:"BC-1f35eadfecb44ba2838fe648c638f71f",
            modules:['pubsub']//根据需要，传入‘pubsub’或'im’，或数组方式同时传入
        });

        // 2、建立连接
        goeasy.connect({
            onSuccess: function () {  //连接成功
                console.log("GoEasy connect successfully.") //连接成功
            },
            onFailed: function (error) { //连接失败
                console.log("Failed to connect GoEasy, code:"+error.code+ ",error:"+error.content);
            },
            onProgress:function(attempts) { //连接或自动重连中
                console.log("GoEasy is connecting", attempts);
            }
        });

        // 3.订阅消息
        var pubsub = goeasy.pubsub;
        pubsub.subscribe({
            channel: "my_channel",// 订阅的通道
            onMessage: function (message) {
                console.log("Channel:" + message.channel + " content:" + message.content);
            },
            onSuccess: function () {
                console.log("Channel订阅成功。");
            },
            onFailed: function (error) {
                console.log("Channel订阅失败, 错误编码：" + error.code + " 错误信息：" + error.content)
            }
        });


    </script>

</head>
<body>
<h3>订阅WebSocket发送的消息</h3>
</body>
</html>